// 公共的全选和反选

function splite (data, flag, acceptanceTable) {
    data.forEach(row => {
        acceptanceTable.toggleRowSelection(row, flag)
        if(row.children != undefined) {
            splite(row.children, flag, acceptanceTable)
        }
    })
}

function acceptanceSelectAll (selection, acceptanceSelects, acceptanceData, acceptanceTable) {
    acceptanceSelects = !acceptanceSelects

    splite(acceptanceData, acceptanceSelects, acceptanceTable)

    // 如果点击全选后,选中数据的长度和原始数组中父级加上changeType不等于-1的子级数据总数相等,就说明是全选选中,否则是全不选中
    let count = 0
    acceptanceData.forEach(item => {
        count++
        item.children.forEach(it => {
            if(it.changeType !== -1) {
                count++
            }
        })
    })
    // console.log(count)
    let num = 0
    selection.forEach(item => {
        if(item.changeType) {
            if(item.changeType !== -1) {
                num++
            }
        }else {
            num++
        }
    })
    // console.log(selection)
    if(num === count) {
        acceptanceSelects = true
    }else {
        acceptanceSelects = false
    }

    // 如果全选按钮是选中状态,那么父级控制选中与否的字段isChecked就应该是true
    if(acceptanceSelects) {
        acceptanceData.forEach(i => {
            i.isChecked = true
        })
    } else {
        acceptanceData.forEach(i => {
            i.isChecked = false
        })
    }
}

export {
    acceptanceSelectAll
}